package com.lingjtx.gencode;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

public class CodeGenerator {

    public static CodeGenerator.Builder init() {
        return new CodeGenerator.Builder();
    }

    public static final class Builder {

        private String jdbcUrl;

        private String username = "root";

        private String password = "123456";

        private String tarPackage;

        private String tarModule;

        private String dtoPackageName = "entity";

        private String javaMapperPackageName = "dao";

        private String xmlMapperPackageName = "mapper";

        public Builder setJdbcUrl(String jdbcUrl) {
            this.jdbcUrl = jdbcUrl;
            return this;
        }

        public Builder setUsername(String username) {
            this.username = username;
            return this;
        }

        public Builder setPassword(String password) {
            this.password = password;
            return this;
        }

        public Builder setTarPackage(String tarPackage) {
            this.tarPackage = tarPackage;
            return this;
        }

        public Builder setTarModule(String tarModule) {
            this.tarModule = tarModule;
            return this;
        }

        public Builder setDtoPackageName(String dtoPackageName) {
            this.dtoPackageName = dtoPackageName;
            return this;
        }

        public Builder setJavaMapperPackageName(String javaMapperPackageName) {
            this.javaMapperPackageName = javaMapperPackageName;
            return this;
        }

        public Builder setXmlMapperPackageName(String xmlMapperPackageName) {
            this.xmlMapperPackageName = xmlMapperPackageName;
            return this;
        }

        public void generator() {
            if (jdbcUrl == null || tarPackage == null || tarModule == null) {
                throw new NullPointerException("jdbcUrl or tarPackage or tarModule is null!");
            }
            try {
                execute(this);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    private static void execute(CodeGenerator.Builder builder) throws SQLException, IOException {
        // 初始化 Velocity 引擎
        VelocityEngine velocityEngine = new VelocityEngine();
        Properties props = new Properties();
        props.put("file.resource.loader.path", "cloud-common/generator-code/src/main/resources/templates");
        velocityEngine.init(props);

        // 初始化数据库连接
        DatabaseUtil connect = new DatabaseUtil(builder.jdbcUrl, builder.username, builder.password);

        // 获取所有表名
        List<String> tableNames = connect.getTableNames();
        for (String tableName : tableNames) {
            // 获取表的字段信息
            List<Column> columns = connect.getColumns(tableName);

            // 创建 Velocity 上下文
            VelocityContext context = new VelocityContext();
            context.put("packageName", builder.tarPackage);
            context.put("className", CommonUtils.toCamelCase(tableName, true));
            context.put("tableName", tableName);
            context.put("columns", columns);
            context.put("javaMapperPackageName", builder.javaMapperPackageName);
            context.put("dtoPackageName", builder.dtoPackageName);

            String target = builder.tarPackage.replace('.', '/');

            // 生成实体类
            CommonUtils.generateFile(velocityEngine, context, "entity.vm",
                    builder.tarModule + "/src/main/java/" + target + "/" + builder.dtoPackageName + "/" + CommonUtils.toCamelCase(tableName, true) + ".java");

            // 生成 DAO 接口
            CommonUtils.generateFile(velocityEngine, context, "dao.vm",
                    builder.tarModule + "/src/main/java/" + target + "/" + builder.javaMapperPackageName + "/" + CommonUtils.toCamelCase(tableName, true) + "Mapper.java");

            // 生成 Mapper XML
            CommonUtils.generateFile(velocityEngine, context, "mapper.vm",
                    builder.tarModule + "/src/main/resources/" + builder.xmlMapperPackageName + "/" + CommonUtils.toCamelCase(tableName, true) + "Mapper.xml");
        }
        // 关闭数据库连接
        connect.close();
    }
}
